package cn.trigram.mgb.plugins;

import java.util.List;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

/**
 * 生成lombok注解
 */
public class LombokPlugin extends PluginAdapter {

  @Override
  public boolean validate(List<String> warnings) {

    return true;
  }

  @Override
  public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

    boolean addBuilder   = Boolean.parseBoolean(properties.getProperty("addBuilder", "true"));
    boolean addAccessors = Boolean.parseBoolean(properties.getProperty("addAccessors", "false"));
    topLevelClass.addAnnotation("@Data");
    topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.Data"));
    if (addBuilder) {
      topLevelClass.addAnnotation("@Builder");
      topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.Builder"));
      topLevelClass.addAnnotation("@AllArgsConstructor");
      topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.AllArgsConstructor"));
      topLevelClass.addAnnotation("@NoArgsConstructor");
      topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.NoArgsConstructor"));
    }
    if (addAccessors) {
      topLevelClass.addAnnotation("@Accessors");
    topLevelClass.addImportedType(new FullyQualifiedJavaType("lombok.experimental.Accessors"));
    }
    return true;
  }

  @Override
  public boolean modelGetterMethodGenerated(
      Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable, ModelClassType modelClassType
  ) {

    return false;
  }

  @Override
  public boolean modelSetterMethodGenerated(
      Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
      IntrospectedTable introspectedTable, ModelClassType modelClassType
  ) {

    return false;
  }

}
